package com.example.javafx.bilibilidownload.contextmenu

import com.example.javafx.bilibilidownload.constant.BiliVideoEnum
import com.example.javafx.bilibilidownload.constant.Myinfo
import com.example.javafx.bilibilidownload.constant.Myinfo.default_begin_time
import com.example.javafx.bilibilidownload.constant.UserAgentConstant.index
import com.example.javafx.bilibilidownload.pojo.BiliFavoriteSimplePojo
import com.example.javafx.bilibilidownload.pojo.BiliFocusPojo
import com.example.javafx.bilibilidownload.pojo.BiliUpPojo
import com.example.javafx.bilibilidownload.pojo.BiliVideoPojo
import com.example.javafx.bilibilidownload.service.BiliServiceImpl
import com.example.javafx.bilibilidownload.util.BiliDownloadUtil
import com.example.javafx.bilibilidownload.util.BiliFileUtil
import com.example.javafx.bilibilidownload.util.BiliFileUtil.FS
import com.example.javafx.bilibilidownload.util.BiliUtil
import com.example.javafx.bilibilidownload.util.BiliUtil.makeVideoNameByCreateTime
import com.example.javafx.bilibilidownload.util.BiliUtil.makeVideoNameByFavoriteTime
import com.example.javafx.bilibilidownload.util.CacheUtil
import com.example.javafx.controller.HomeController
import com.example.javafx.controller.MainController
import javafx.application.Platform
import javafx.concurrent.Task
import javafx.scene.control.ContextMenu
import javafx.scene.control.MenuItem
import javafx.scene.control.TreeItem
import java.awt.Desktop
import java.util.function.Consumer

object ContextMenuUtil {

    val biliServiceImpl = BiliServiceImpl()

    // 创建菜单项
    val refreshFocusList = MenuItem("刷新分组信息").apply {
        setOnAction {
            var homeController = CacheUtil.getFxmlController(HomeController::class.java, "/fxml/home.fxml") as HomeController
            var mainController = CacheUtil.getFxmlController(MainController::class.java, "/fxml/main.fxml") as MainController
            var task = object : Task<Unit>() {
                override fun call() {

                    if (homeController.tree.selectionModel.selectedItem.value.equals("我的关注")) {
                        biliServiceImpl.refreshMyfocus(Myinfo.id)
                        Platform.runLater {
                            mainController.refresh.fire()
                        }
                    }
                    if (homeController.tree.selectionModel.selectedItem.value.equals("我的收藏夹")) {
                        val refreshMyFavorite = biliServiceImpl.refreshMyFavorite()
                        refreshMyFavorite.forEach {
                            biliServiceImpl.refreshMyFavoriteDetail(it)
                        }
                        Platform.runLater {
                            mainController.refresh.fire()
                        }
                    }
                }
            }
            Thread(task).start()
        }
    }

    // 创建菜单项
    val refreshUpList = MenuItem("刷新Up信息").apply {
        setOnAction {
            var homeController = CacheUtil.getFxmlController(HomeController::class.java, "/fxml/home.fxml") as HomeController
            var value = homeController.tree.selectionModel.selectedItem.value as BiliFocusPojo

            var task = object : Task<Unit>() {
                override fun call() {
                    biliServiceImpl.refreshFocusUpList(value)
                    Platform.runLater {
                        //HomeController.refresh.fire()
                    }
                }
            }
            Thread(task).start()
        }
    }

    // 创建菜单项
    val refreshFavoriteList = MenuItem("刷新收藏夹信息").apply {
        setOnAction {
            var homeController = CacheUtil.getFxmlController(HomeController::class.java, "/fxml/home.fxml") as HomeController
            var value = homeController.tree.selectionModel.selectedItem.value as BiliFavoriteSimplePojo

            var task = object : Task<Unit>() {
                override fun call() {
                    biliServiceImpl.refreshMyFavoriteDetail(value)
                    Platform.runLater {
                        var favoriteVideoList = biliServiceImpl.getMyFavoriteDetail(value)
                        homeController.tree.selectionModel.selectedItem.children.clear()
                        favoriteVideoList.forEach {
                            var videoItem = TreeItem<Any>(it)
                            homeController.tree.selectionModel.selectedItem.children.add(videoItem)
                            // 如果是合集视频，则要拿到合集列表
                            if (!it.collectionName.isNullOrEmpty()) {
                                val collectionVideoList = biliServiceImpl.getMyFavoriteCollectionDetail(it)
                                collectionVideoList.forEach {
                                    var collectVideoItem = TreeItem<Any>(it)
                                    videoItem.children.add(collectVideoItem)
                                }
                            }
                        }
                    }
                }
            }
            Thread(task).start()
        }
    }

    // 创建菜单项
    val refreshUpAndVideoList = MenuItem("刷新Up信息和视频信息").apply {
        setOnAction {
            var homeController = CacheUtil.getFxmlController(HomeController::class.java, "/fxml/home.fxml") as HomeController
            var value = homeController.tree.selectionModel.selectedItem.value as BiliFocusPojo

            var task = object : Task<Unit>() {
                override fun call() {
                    biliServiceImpl.refreshFocusUpList(value)
                    biliServiceImpl.getFocusUpList(value.focusName).forEach {
                        biliServiceImpl.refreshUPAllVideoList(it.upId, it.upName)
                    }
                    Platform.runLater {
                        //HomeController.refresh.fire()
                    }
                }
            }
            Thread(task).start()
        }
    }


    val refreshUpVideoList = MenuItem("刷新视频信息").apply {
        setOnAction {
            var homeController = CacheUtil.getFxmlController(HomeController::class.java, "/fxml/home.fxml") as HomeController
            var value = homeController.tree.selectionModel.selectedItem.value as BiliUpPojo

            var task = object : Task<Unit>() {
                override fun call() {
                    biliServiceImpl.refreshUPAllVideoList(value.upId, value.upName)
                    // 获取up的所有视频
                    val upAllVideoList = biliServiceImpl.getUPAllVideoList(value.upName, 0, default_begin_time, emptyList())
                    homeController.tree.selectionModel.selectedItem.children.clear()
                    upAllVideoList.forEach {
                        var videoItem = TreeItem<Any>(it)
                        Platform.runLater {
                            homeController.tree.selectionModel.selectedItem.children.add(videoItem)
                        }
                    }
                }
            }
            Thread(task).start()
        }
    }

    val downloadUpAllVideo = MenuItem("下载全部视频").apply {
        setOnAction {
            var homeController = CacheUtil.getFxmlController(HomeController::class.java, "/fxml/home.fxml") as HomeController
            var value = homeController.tree.selectionModel.selectedItem.value as BiliUpPojo

            var task = object : Task<Unit>() {
                override fun call() {
                    BiliDownloadUtil.downloadUp(value.upId, value.upName, 0, default_begin_time, listOf())
                }
            }
            Thread(task).start()
        }
    }

    val downloadVideo = MenuItem("下载视频").apply {
        setOnAction {
            var homeController = CacheUtil.getFxmlController(HomeController::class.java, "/fxml/home.fxml") as HomeController
            var value = homeController.tree.selectionModel.selectedItem.value as BiliVideoPojo

            var task = object : Task<Unit>() {
                override fun call() {

                    if (value.favoriteName.isNullOrEmpty()) {
                        biliServiceImpl.fillVideoAndAudioInfos(listOf(value), BiliVideoEnum.Up, BiliFileUtil.getFavoriteErrorFile(), true)
                        makeVideoNameByCreateTime(value)
                        BiliDownloadUtil.donwloadByExecutor(listOf(value), BiliVideoEnum.Up)
                    } else {
                        if (value.collectionName.isNullOrEmpty()) {
                            biliServiceImpl.fillVideoAndAudioInfos(listOf(value), BiliVideoEnum.Favorite, BiliFileUtil.getFavoriteErrorFile(), true)
                            makeVideoNameByFavoriteTime(value)
                            BiliDownloadUtil.donwloadByExecutor(listOf(value), BiliVideoEnum.Favorite)
                        } else {
                            // 下载合集视频
                            val myFavoriteCollectionDetail = biliServiceImpl.getMyFavoriteCollectionDetail(value);
                            myFavoriteCollectionDetail.forEach {
                                BiliUtil.makeVideoNameByNull(it)
                            }
                            biliServiceImpl.fillVideoAndAudioInfos(myFavoriteCollectionDetail, BiliVideoEnum.Favorite, BiliFileUtil.getFavoriteErrorFile(), true)
                            BiliDownloadUtil.donwloadByExecutor(myFavoriteCollectionDetail, BiliVideoEnum.Favorite)
                        }
                    }
                }
            }
            Thread(task).start()
        }
    }

    val playVideo = MenuItem("播放视频").apply {
        setOnAction {
            var homeController = CacheUtil.getFxmlController(HomeController::class.java, "/fxml/home.fxml") as HomeController
            var value = homeController.tree.selectionModel.selectedItem.value as BiliVideoPojo

            var task = object : Task<Unit>() {
                override fun call() {
                    Platform.runLater {


                        if (value.favoriteName.isNullOrEmpty()) {
                            makeVideoNameByCreateTime(value)
                            val path = BiliFileUtil.getResult(BiliVideoEnum.Up, value, value.author)
                            // 创建新舞台
                            // 获取桌面对象
                            val desktop = Desktop.getDesktop()

                            // 使用默认应用程序打开文件
                            desktop.open(BiliFileUtil.getAbsoluteFile(path.completedVideoPath))
                        } else {
                            makeVideoNameByFavoriteTime(value)
                            val path = BiliFileUtil.getResult(BiliVideoEnum.Favorite, value, value.author)
                            // 创建新舞台
                            // 获取桌面对象
                            val desktop = Desktop.getDesktop()
                            // 使用默认应用程序打开文件
                            desktop.open(BiliFileUtil.getAbsoluteFile(path.completedVideoPath))
                        }
                    }
                }
            }
            Thread(task).start()
        }
    }

    val deleteVideo = MenuItem("删除视频").apply {
        setOnAction {
            var homeController = CacheUtil.getFxmlController(HomeController::class.java, "/fxml/home.fxml") as HomeController
            var value = homeController.tree.selectionModel.selectedItem.value as BiliUpPojo

            var task = object : Task<Unit>() {
                override fun call() {
                    biliServiceImpl.refreshUPAllVideoList(value.upId, value.upName)
                    Platform.runLater {
                        //HomeController.refresh.fire()
                    }
                }
            }
            Thread(task).start()
        }
    }

    fun getFocusMenu(): ContextMenu {
        return ContextMenu().apply {
            items.addAll(refreshFocusList)
        }
    }

    fun getFocusGroupMenu(): ContextMenu {
        return ContextMenu().apply {
            items.addAll(refreshUpList, refreshUpAndVideoList)
        }
    }

    fun getFavoriteGroupMenu(): ContextMenu {
        return ContextMenu().apply {
            items.addAll(refreshFavoriteList)
        }
    }

    fun getUpMenu(): ContextMenu {
        return ContextMenu().apply {
            items.addAll(refreshUpVideoList, downloadUpAllVideo)
        }
    }

    fun getVideoMenu(): ContextMenu {
        return ContextMenu().apply {
            items.addAll(downloadVideo, playVideo, deleteVideo)
            setOnShowing {
                var homeController = CacheUtil.getFxmlController(HomeController::class.java, "/fxml/home.fxml") as HomeController
                var value = homeController.tree.selectionModel.selectedItem?.value as BiliVideoPojo
                // 判断是否为普通Up视频
                if (value.favoriteName.isNullOrEmpty()) {
                    // 判断文件是否下载，没有下载才能下载，下载才能播放
                    makeVideoNameByCreateTime(value)
                    val path = BiliFileUtil.getResult(BiliVideoEnum.Up, value, value.author)
                    if (BiliFileUtil.checkFileExist(path.completedVideoPath)) {
                        downloadVideo.isDisable = true
                        playVideo.isDisable = false
                        deleteVideo.isDisable = false
                    } else {
                        downloadVideo.isDisable = false
                        playVideo.isDisable = true
                        deleteVideo.isDisable = true
                    }
                }
                // 判断是否为收藏视频
                else {
                    // 如果是集合视频目录本身，就算了
                    if (value.title.equals(value.collectionName)) {
                        if (BiliFileUtil.checkFileExist(BiliFileUtil.getFavoriteCompleteConvertFile(value.favoriteName, value.title))) {
                                downloadVideo.isDisable = true
                            } else {
                            downloadVideo.isDisable = false
                        }
                        playVideo.isDisable = true
                        deleteVideo.isDisable = true
                        return@setOnShowing
                    }
                    // 判断文件是否下载，没有下载才能下载，下载才能播放
                    makeVideoNameByFavoriteTime(value)
                    val path = BiliFileUtil.getResult(BiliVideoEnum.Favorite, value, value.author)
                    if (BiliFileUtil.checkFileExist(path.completedVideoPath)) {
                        downloadVideo.isDisable = true
                        playVideo.isDisable = false
                        deleteVideo.isDisable = false
                    } else {
                        downloadVideo.isDisable = false
                        playVideo.isDisable = true
                        deleteVideo.isDisable = true
                    }
                }
                // 判断是否为收藏视频
            }
        }
    }
}